// ==UserScript==
// @name xy139.answer
// @author wgu
// @description  xy139
// @include http://xq.139life.com/jsdt/answer_question.jsp*
// @dbfile XQ.sqlite
// ==/UserScript==
function addId(na){
	if(document.getElementsByName(na)){
		document.getElementsByName(na)[0].id=na;
	}
}
addId('method');
addId('rownum');

document.getElementById('nextquestion').onclick='';
document.getElementById('nextquestion').href='javascript:next()';

var _g=0,_t={'i':[],'u':[],'q':[],'q2':[]};
var pref = GM_Pref('wgu.xq139life.');
pref.setValue('_t','');

function getRadioValue(radioName){
	var radios=document.getElementsByName(radioName),i=0,l=radios.length;
    for(var i=0;i<l;i++){
        if(radios[i].checked)
		return radios[i].value;
    }
	return '';
} 


var next=unsafeWindow.next=function(){
	console.log('next');
	document.getElementById("questionhelp").style.display="none";
	if(unsafeWindow.maxtime1>=0){
		if(getRadioValue("question")==""){
			document.forms[2].action ='./selectRadioMessage.jsp?rownum='+$('rownum').value;
			document.forms[2].submit();
		}else{
			var rownum =document.getElementById("rownum").value;
			document.getElementById("rownum").value = parseInt(rownum)+1;
			getQuestion();
		}
	}else{
		document.forms[1].submit();
	}
}




var getQuestion=unsafeWindow.getQuestion=function (){
	var req=new XMLHttpRequest();
	function _send(){
		req.open('POST', 'http://xq.139life.com/jsdt/question_control.jsp', false); 
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send('flag=getQuestion&rownum='+document.getElementById("rownum").value+'&selectRadio='+getRadioValue("question")+'&_method=getquestion');
		if(req.status != 200){
			_send();
		}
	}
	_send();
	
	var jsonObj = eval('(' + req.responseText + ')');

	document.getElementById('questionInfo').innerHTML=jsonObj.options[0].title;
	document.getElementById('itemInfo').innerHTML=jsonObj.options[0].itemInfo;
	document.getElementById('rownum').value = jsonObj.options[0].rownum;

	if(jsonObj.options[0].help!=""){
		document.getElementById('qiuzhuContext').value = jsonObj.options[0].help;
		document.getElementById('qiuzhu').style.display ="inline";
	}else{
		document.getElementById('questionhelp').style.display ="none";
		document.getElementById('qiuzhu').style.display ="none";
	}
	
	if(jsonObj.options[0].rownum==14){
		document.getElementById('completequestion').style.display ="inline-block";
		document.getElementById('nextquestion').style.display ="none";
	}else{
		document.getElementById('completequestion').style.display ="none";
	}

	var q=document.querySelectorAll('.content_timu')[0].innerHTML.replace(/\s+/g,'');
	var a=document.querySelectorAll('.content_xuan p'),l=a.length,i=0,an='';
	for (i=0;i<l;i++){
		an+='#'+a[i].textContent.replace(/\s+/g,'');
	}

	var xqDB = GM_DB('XQ.sqlite');
	xqDB.open();
	var data=xqDB.getData("select * from XQ where q ='"+q+an+"';",[0,1,1,1,1]);
	xqDB.close();
	
	if(data.length==0){
		document.querySelectorAll('.content_xuan input')[1].checked=true;
		if(jsonObj.options[0].help!=""){
			console.log('*************'+jsonObj.options[0].help);
		}
		console.log(",'"+q+an+"@'");
		_t['q2'].push(q+an+'@1');
		console.log('@no exit');
	}else{
		document.querySelectorAll('.content_xuan input')[data[0][1]].checked=true;
		
		if(data[0][2]==0&&jsonObj.options[0].help!=""){
			console.log('$$$$$$$$$$$$$$'+jsonObj.options[0].help);
			console.log(",'"+q+an+"@"+data[0][1]+"'");
		}
		if(data[0][3]>=1){
			_t['q'].push(q+an+'@'+data[0][1]);
		}else{
			_t['q2'].push(q+an+'@'+data[0][1]);
		}
	}
	
	document.getElementById("falgs").value ="falgs";
	
	if(jsonObj.options[0].rownum>8){
		document.querySelectorAll('.content_xuan input')[1].checked=true;
	}
	if(jsonObj.options[0].rownum<14){
		next();
	}else{
		pref.setValue('_t',JSON.stringify(_t));
		complete();	
	}
	
}

function complete(){
	if(unsafeWindow.maxtime1>0){
		if(getRadioValue("question")==""){
			document.forms[2].action ="./selectRadioMessage.jsp?rownum="+document.getElementById('rownum').value;
			document.forms[2].submit();
		 }else{
			document.forms[0].action='./answer_question_control.jsp?rownum='+document.getElementById('rownum').value;
			document.forms[0].submit();
		}
	}
}
